package io.renren.modules.se.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.se.dao.LendListDao;
import io.renren.modules.se.dto.LendListDTO;
import io.renren.modules.se.entity.LendListEntity;
import io.renren.modules.se.service.LendListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 器材出借记录表
 *
 * 
 * 
 */
@Service
public class LendListServiceImpl extends CrudServiceImpl<LendListDao, LendListEntity, LendListDTO> implements LendListService {

    @Autowired
    private LendListDao lendListDao;

    @Override
    public QueryWrapper<LendListEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<LendListEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    //获取当前用户在出借记录
    @Override
    public Integer getOutInfo(String eid, String uid) {
        LendListEntity lendListEntity = lendListDao.selectOne(new QueryWrapper<LendListEntity>().eq("eid", eid).eq("uid", uid));
        if (lendListEntity==null){
            return null;
        }else {
            return lendListEntity.getOutNum();
        }
    }

    //连表查询
    @Override
    public PageData<LendListDTO> pageWithEandU(Map<String, Object> params) {

        //转换成like
        paramsToLike(params, "username");
        //分页
        IPage<LendListEntity> page = getPage(params, Constant.CREATE_DATE, false);
        //查询
        List<LendListEntity> list = lendListDao.pageWithEandU(params);

        return getPageData(list, page.getTotal(), LendListDTO.class);
    }
}